program define levels2_byparty
   version 15.1	
	* future: extend to allow label1 label2 and legend	
	* Added byopts option 1/30/2021
	syntax varlist [if] [pweight], GP1(varname) GP2(varname) [OPTS(str) BYOPTS(str) ALLFIVE]
	marksample touse
	
	if "`weight'" ~= "" {
	   local wt [`weight'`exp']
   }	
	
   forvalues p = 1/3 {
	   di _n(2) "** Party `p' **"
      levels2 `varlist' if `touse' & pid3==`p' `wt', gp1(`gp1') gp2(`gp2') ///
		   opts(`opts') `allfive' nodraw
		tempname lvl1`p' lvl2`p'
      mat `lvl1`p'' = r(lvl1)
      mat `lvl2`p'' = r(lvl2)
   }

	* combine three plots horizontally
	local gopts scheme(s1mono) plotregion(style(none)) mlabel ///
	   format(%3.0f) xlabel(0(20)100)
	coefplot (matrix(`lvl11'[1,]), ci("`lvl11'[5,] `lvl11'[6,]") msymbol(O) mlabpos(2))  ///
		(matrix(`lvl21'[1,]), ci("`lvl21'[5,] `lvl21'[6,]") msymbol(Oh) mlabpos(10) /// 
		mcolor(gs8) mlabcolor(gs8)), bylabel(Democrats) ///
		||  ///
		(matrix(`lvl12'[1,]), ci("`lvl12'[5,] `lvl12'[6,]") msymbol(O) mlabpos(2))  ///
		(matrix(`lvl22'[1,]), ci("`lvl22'[5,] `lvl22'[6,]") msymbol(Oh) mlabpos(10) /// 
		mcolor(gs8) mlabcolor(gs8)), bylabel(Independents) ///
		|| ///
		(matrix(`lvl13'[1,]), ci("`lvl13'[5,] `lvl13'[6,]") msymbol(O) mlabpos(2))  ///
		(matrix(`lvl23'[1,]), ci("`lvl23'[5,] `lvl23'[6,]") msymbol(Oh) mlabpos(10) /// 
		mcolor(gs8) mlabcolor(gs8)), bylabel(Republicans) ///
		coeflabel(none = "No Action" low = "Cut 5%" high = "Cut 25%") /// only applies if not all 5
		`gopts' `opts' ///
		byopts(rows(1) legend(off) `byopts') subtitle(, fcolor(white))
	
end
